<template>
  <ElementDialog
    ref="dialog"
    embed
    height="100%"
    width="60%"
    show-full-screen
    :visible="visible"
    title="采购申请单"
    @open="onOpen">
    <FyCurd
      ref="crud"
      v-model="form"
      :page.sync="page"
      :data="tableData"
      :table-loading="tableLoading"
      :option="tableOption"
      right-search
      @on-load="getList"
      @search-change="searchChange"
      @refresh-change="refreshChange"
      @size-change="sizeChange"
      @before-open="handleOpenBefore"
      @after-open="handleOpenBefore"
      @current-change="currentChange"
      @expand-change="expandChange"
      @selection-change="handleSelectionChange">
      <template #expand="{ row }">
        <ElTable
          ref="multipleTable"
          :data="row.purchaseapplyList"
          tooltip-effect="dark"
          style="width: 100%"
          cell-click="cellDblclick"
          @selection-change="handleSelectionChangeChild">
          <TableColumn type="selection" width="55" />
          <TableColumn prop="photo" label="照片" width="120">
            <template #default="scope">
              <img
                v-if="scope.row.photo"
                :src="scope.row.photo"
                class="avatar"
                alt=""
                style="width: 120px; height: 80px" />
            </template>
          </TableColumn>
          <TableColumn prop="name" label="姓名" width="120" />
          <TableColumn prop="ctiName" label="资产分类名称" width="120" />
          <TableColumn prop="barcode" label="条型码" width="120" />
          <TableColumn prop="name" label="资产名称" width="120" />
          <TableColumn prop="model" label="规格型号" width="120" />
          <TableColumn prop="unit" label="计量单位" />
          <TableColumn prop="quantity" label="申请数量" />
          <TableColumn prop="purchasedQuantity" label="已采购数量" />
          <TableColumn prop="grantQuantity" label="已发放数量" />
        </ElTable>
      </template>
    </FyCurd>
    <template #footer>
      <ElButton type="primary" @click="onConfirm">确定</ElButton>
    </template>
  </ElementDialog>
</template>

<script>
  import { tablePageOption } from '@/const/crud/asset/fyassetpurchaseapply'
  import { getApplyPage } from '@/api/asset/fyassetpurchaseapply'

  export default {
    name: 'SelectApplyListTable',
    props: {
      joinPurchaseorder: {
        type: Boolean
      },
      visible: Boolean
    },
    data() {
      return {
        tableData: [],
        searchForm: {},
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20 // 每页显示多少条
        },
        tableLoading: false,
        tableOption: tablePageOption,
        form: {},
        selectedRows: [],
        selectedListRows: []
      }
    },
    computed: {},
    created() {
      this.init()
    },
    methods: {
      init() {},
      // 列表查询
      getList(page, params) {
        this.tableLoading = true
        if (this.$isBlank(params)) {
          params = { joinPurchaseorder: this.joinPurchaseorder }
        } else {
          params.joinPurchaseorder = this.joinPurchaseorder
        }
        params.status = '2'
        getApplyPage(
          Object.assign(
            {
              current: page.currentPage,
              size: page.pageSize
            },
            params,
            this.searchForm
          )
        )
          .then(response => {
            this.tableData = response.data.data.records
            this.page.total = response.data.data.total
            this.tableLoading = false
          })
          .catch(() => {
            this.tableLoading = false
          })
      },
      // 每页条数改变事件
      sizeChange(pageSize) {
        this.page.pageSize = pageSize
      },
      // 当前页发生改变事件
      currentChange(current) {
        this.page.currentPage = current
      },
      // 查询事件
      searchChange(form, done) {
        this.searchForm = form
        this.page.currentPage = 1
        this.getList(this.page, form)
        done()
      },
      // 刷新事件
      refreshChange() {
        this.getList(this.page)
      },
      handleOpenBefore(show, type) {
        window.boxType = type
        show()
      },
      onConfirm() {
        if (this.selectedRows.length <= 0) {
          this.$message.success('请选择采购订单！')
          return
        }
        if (this.selectedListRows.length <= 0) {
          this.$emit('confirm', this.selectedRows[0].purchaseapplyList)
        } else {
          this.$emit('confirm', this.selectedListRows)
        }
        this.$refs.dialog.hide()
        this.tableOption.expandRowKeys = []
        this.selectedRows.forEach(row => this.$refs.crud.toggleRowSelection(row, false))
      },
      getPage(page) {
        return getApplyPage({ current: page.page, size: page.limit })
      },
      onChange(rows) {
        this.selectedRows = rows
      },
      expandChange(row, expendList) {
        // 关闭展开消取选中
        if (expendList <= 0) {
          this.$refs.crud.toggleRowSelection(row, false)
        }
        if (expendList.length) {
          this.tableOption.expandRowKeys = []
          if (row) {
            this.tableOption.expandRowKeys.push(row.caseid)
            this.$refs.crud.toggleRowSelection(row, true)
          }
        } else {
          this.tableOption.expandRowKeys = []
        }
      },
      handleSelectionChange(val) {
        this.tableData.forEach(item => {
          if (val[val.length - 1] === item) {
            this.tableOption.expandRowKeys = []
            this.tableOption.expandRowKeys.push(item.caseid)
            this.$refs.crud.toggleRowSelection(item, true)
          } else {
            this.$refs.crud.toggleRowSelection(item, false)
          }
        })
        this.selectedRows = this.$refs.crud.tableSelect
      },
      handleSelectionChangeChild(val) {
        this.selectedListRows = this.$refs.multipleTable.selection
      },
      onOpen() {}
    }
  }
</script>
